

LogCreatedMessage = function(body) {
	ClientMessage.call(this);
	this.body = body;
};

LogCreatedMessage.prototype = new ClientMessage;

LogCreatedMessage.prototype.as_json = function() {
    var result = ClientMessage.prototype.as_json.call(this);
    result.body = this.body;
    return result;
};

LogPublishedMessage = function(text) {
	ServerMessage.call(this);
	this.body = body;
};

LogPublishedMessage.prototype = new ServerMessage;

LogPublishedMessage.prototype.perform = function() {
	updateMessageList(this.body);
};
LogPublishedMessage.prototype.build = function(payload) {
	return new LogPublishedMessage(payload.body);
};

var updateMessageList = function(messageBody) {
	var messageList = document.getElementById('list');
	var item = document.createElement('li');
	var itemContent = document.createTextNode(messageBody);
	item.addChild(itemContent);
	messageList.addChild(item);
}

var attachButtonAction = function() {
	var inputSource = document.getElementById('text-source');
	var sendButton = document.getElementById('button');
	sendButton.onclick = function() {
		var messageBody = inputSource.value;
		var sentMessage = new LogCreatedMessage(messageBody);
		sentMessage.send();
		inputSource.value = '';
		return false;
	};
}

init.execute(attachButtonAction);


REGISTERED = [LogPublishedMessage]; // MARK TODO
BUILDER = new MessageBuilder(REGISTERED);

LOCATION = "ws://localhost:8080/channel"; // MARK

CHANNEL = new Channel(BUILDER);
init.execute(function () {
	CHANNEL.connect(LOCATION);
});

finalize.execute(function() {
	CHANNEL.close();
});

